package lvxu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lvxu.bean.*;
import lvxu.bean.Song;
import lvxu.service.MusicListService;
import lvxu.service.SongService;
import lvxu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.tools.Diagnostic;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MusicListController {
    @Autowired
    MusicListService musicListService;
    @Autowired
    UserService userService;
    @Autowired
    SongService songService;
    //添加用户信息
    @RequestMapping(value = "/musicadd",method = RequestMethod.POST)
    @ResponseBody
    public Msg addMusic(Music music){
        System.out.println("你好==="+music+"不好");
        boolean b = musicListService.saveMusic(music);
        return b?Msg.success():Msg.fail();
    }
    //修改某一歌曲信息
    @RequestMapping(value = "/music",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateMusic(Music music){
        System.out.println("你好==="+music+"不好");
        Music new_music = musicListService.updateMusic(music);
        return Msg.success();
    }
    //查询歌曲
    @RequestMapping(value = "/musicone/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectMusicById(@PathVariable("id") Integer id){
        //1、查询该歌曲信息 查询用户信息
        Music music = musicListService.selectByMusicId(id);
        return Msg.success().add("music",music);
    }
    //歌曲的删除
    @RequestMapping(value = "/music/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteMusicById(@PathVariable("id")Integer id){
        boolean delete = musicListService.deleteById(id);
        //添加信息到我的歌单中
        return delete?Msg.success():Msg.fail();
    }
    //查询歌曲列表
    @RequestMapping(value = "/musicall",method = RequestMethod.GET)
    @ResponseBody
    public Msg addSongList(){
        //1、查询该歌曲信息 查询用户信息
        List<Music> musicList = musicListService.selectByAll();
        //添加信息到我的歌单中
        return Msg.success().add("musics",musicList);
    }


    @RequestMapping(value = "/music/{kind}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getMusicInfo(@PathVariable("kind") Integer kind,@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        //每页的分页数
        PageHelper.startPage(pn,8);
        List<Music> musicList = new ArrayList<>();
        if(kind == 0){
            musicList = musicListService.selectByAll();
        }else {
            musicList = musicListService.selectByKind(kind);
        }
        //导航页码数
        PageInfo<Music> pageInfo = new PageInfo<>(musicList,5);
        return Msg.success().add("music",pageInfo);
    }
    //修改收藏状态
    @RequestMapping(value = "/music/{musicid}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateMusicLike(@PathVariable("musicid") Integer musicid, @RequestParam("userid") Integer userid){
        boolean updateMusicLike = false;
//        System.out.println("你好===============");
        int flag = 0;
        //2、查询该歌曲信息 查询用户信息
        Music music = musicListService.selectByMusicId(musicid);
//        System.out.println("music-----------------------music" + music);

        User user = userService.selectByUserId(userid);
//        System.out.println("user-----------------------user" + user);

        List<Song> song = songService.selectByUserId(user.getUserid());
//        System.out.println("-----------------------" + song);
        Song collect = new Song(user.getUserid(),music.getMusicid(),0);
        for (Song c:song){
            if(music.getMusicid() == c.getMusicid() && user.getUserid() == c.getUserid() && c.getState() == 0){
                //如果该歌曲该用户已收藏 则删除
                songService.deleteById(c.getMusicid(),0);
//                System.out.println("==========================================");
                updateMusicLike = musicListService.updateMusicLike(musicid,"del");
//                System.out.println("==========================================");
                flag = 1;
                break;
            }
        }
        if (flag == 0){
            songService.insertSong(collect);
            //收藏歌曲的数量加一
            //1、修改状态并添加歌曲到收藏列表
            System.out.println("==========================================");

            updateMusicLike = musicListService.updateMusicLike(musicid,"add");
            System.out.println("==========================================");

        }
//        System.out.println(userid);
        //3、判断该歌曲收藏状态是否是选定，是，将其添加到收藏列表，否，将收藏列表中的该歌曲信息删除
        return updateMusicLike? Msg.success():Msg.fail();
    }
    //添加歌曲到歌单列表
    @RequestMapping(value = "/music/{musicid}",method = RequestMethod.POST)
    @ResponseBody
    public void addSongList(@PathVariable("musicid") Integer musicid, @RequestParam("userid") Integer userid){
        //1、查询该歌曲信息 查询用户信息
        Music music = musicListService.selectByMusicId(musicid);
        User user = userService.selectByUserId(userid);
        Song songList = new Song(user.getUserid(),music.getMusicid(),1);
        //添加信息到我的歌单中
        songService.addMusic(songList);
    }
}
